package com.kingmed.kmss.common.utils;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Service;

/**
 * 
 *
 */

@Service
public class AppContextUtil implements ApplicationContextAware {

	private static ApplicationContext appContext;

	@Override
	public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
		appContext = applicationContext;
	}

	/**
	 * 根据Bean的名字，从Spring Bean 容器库中取得指定Bean的实例
	 * 
	 * @param beanName
	 * @return
	 */
	public static Object getBean(String beanName) {
		return appContext.getBean(beanName);
	}

	/**
	 * 根据类定义，取得Bean的实例
	 * 
	 * @param clazz
	 * @return
	 */
	public static <T> T getBean(Class<T> clazz) {
		return appContext.getBean(clazz);
	}
}
